 < !DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>分组管理</title>
<link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
<link href="../../css/style.css" rel="stylesheet" type="text/css">
<script src="../../layui/layui.js"></script>
<script src="../../js/jquery-1.8.3.js"></script>
<script src="../../js/util.js"></script>
<script>
	function addUser() {
		$.ajax({
			type : 'post',
			async : false,
			url : '/util/findUser',
			success : function(json) {
				user = json;
				if (json.duty != 3) {
					location.href = "smsGroupAdd.html";
				} else {
					layer.open({
						content : '权限不足',
						scrollbar : false
					});
				}
			},
			error : function() {
				layer.msg('系统异常，请联系管理员处理!');
			}
		});
	}

	layui.use([ 'table', 'form', 'jquery', 'element' ], function() {
		var table = layui.table;
		var form = layui.form;
		var $ = layui.$;

		var tabledata = {
			elem : "#table", //表格id
			id : "modelReload", //id
			url : '/smsMgrGroup/qryGroupList',
			method : 'post',
			request : {
				pageName : 'page',
				limitName : 'limit'
			},
			response : {
				statusName : 'code',
				statusCode : 0,
				msgName : 'msg',
				countName : 'count',
				dataName : 'data'
			},
			page : true,
			count : "count",
			limit : 10,
			limits : [ 10, 20, 30, 40, 50 ],
			layout : [ 'prev', 'page', 'next' ],
			prev : "上一页",
			next : "下一页",
			cols : [ [ //表头
				{
					field : 'belong',
					title : '地市/区县'
					,align: 'center'
				}
				, {
					field : 'groupName',
					title : '分组名称'
					,align: 'center'
				}
				, {
					field : 'saveDate',
					title : '发送保存时间（天）'
					,align: 'center'
				}
				, {
					field : 'createOperator',
					title : '创建人'
					,align: 'center'
				}
				, {
					field : 'createDate',
					title : '创建时间'
					,align: 'center'
				}
				, {
					field : 'operation',
					title : '操作',
					toolbar : '#barDemo'
					,align: 'center'
				}
			] ],
			done : function(res) {
			}
		};
		table.render(tabledata);

		function selectduty() { //点击按钮触发权限控制操作

			$.ajax({
				type : 'post',
				async : false,
				url : '/util/findUser',
				success : function(json) {
					user = json;
					if (json.duty != 0) {
							table.on('tool(demo)', function(obj) {
								var data = obj.data;
								console.log(data)
								if (obj.event === 'del') {
									layer.confirm('您确定要删除吗', {
										icon : 3,
										title : '提示'
									}, function(index) {
										var url = "/smsMgrGroup/deleteMgrGroup";
										var param = {
											groupId : data.groupId
										};
										$.ajax({
											type : 'post',
											data : param,
											url : url,
											beforeSend : function() {},
											success : function(data) {
												layui.use('layer', function() {
													if (data.code == 0) {
														layer.confirm('删除成功', {
															btn : [ '确认' ] //按钮
														}, function() {
															location.href = "smsMgrManage.html";
														});
													} else {
														layer.confirm('删除失败', {
															btn : [ '确认' ] //按钮
														}, function() {
															location.href = "smsGroupManage.html";
														});
													}
												});
											},
											complete : function() {},
											error : function(data) {
												layer.msg('系统异常，请联系管理员处理!');
											}
										});
									});

								} else if (obj.event === 'edit') {
									console.info("---"+JSON.stringify(data));
									console.info("---"+data.groupId);
									location.href = "smsGroupModify.html?groupId=" + data.groupId;
								}else if (obj.event === 'view') {
									location.href = "smsGroupView.html?groupId=" + data.groupId;
								}
							});
					} else {
						table.on('tool(demo)', function(obj) {
							var data = obj.data;
							if (obj.event === 'del') {
								layer.confirm('您确定要删除吗', {
									icon : 3,
									title : '提示'
								}, function(index) {
									var url = "/smsMgrGroup/deleteMgrGroup";
									var param = {
										groupId : data.groupId
									};
									$.ajax({
										type : 'post',
										data : param,
										url : url,
										beforeSend : function() {},
										success : function(data) {
											layui.use('layer', function() {
												if (data.code == 0) {
													layer.confirm('删除成功', {
														btn : [ '确认' ] //按钮
													}, function() {
														location.href = "smsGroupManage.html";
													});
												} else {
													layer.confirm('删除失败', {
														btn : [ '确认' ] //按钮
													}, function() {
														location.href = "smsGroupManage.html";
													});
												}
											});
										},
										complete : function() {},
										error : function(data) {
											layer.msg('系统异常，请联系管理员处理!');
										}
									});
								});

							} else if (obj.event === 'edit') {
								console.info(data.groupId);
								location.href = "smsGroupModify.html?groupId=" + data.groupId;
							}else if (obj.event === 'view') {
								location.href = "smsGroupView.html?groupId=" + data.groupId;
							}
						});
					}

				},
				error : function() {
					layer.msg('系统异常，请联系管理员处理!');
				}
			});
		}

		//监听区县
		form.on('select(cityId)', function(data) {
			var data1 = selectCountyId(data.value);
			appendCounty(data1);
		});
		//根据城市ID查询区县
		function selectCountyId(cityId) {
			var data;
			var url = '/util/findCountyByCityId?cityId=' + cityId;
			$.ajax({
				type : 'post',
				async : false,
				url : url,
				success : function(json) {
					data = json.data;
				},
				error : function() {
					layer.msg('系统异常，请联系管理员处理!');
				}
			}
			);
			return data;
		}
		//动态拼接区县
		function appendCounty(data) {
			var options = '<option value="">全部</option>';
			if (data.length != null) {
				for (var i = 0; i < data.length; i++) {
					options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
				}
				$('#countyId').html(options);
			} else {
				$('#countyId').html('该地市下没有区县');
			}
			form.render();
		}

		//身份识别
		$(document).ready(function() {
			$.ajax({
				type : 'post',
				async : false,
				url : '/util/findUser',
				success : function(json) {
					user = json;
					if (json.duty == 0) { //省管理员

					} else if (json.duty == 1) { //地市管理员
						$("#cityId").val(json.cityId);
						$("#cityId").attr("disabled", true);
						var data1 = selectCountyId(json.cityId);
						appendCounty(data1);
						form.render();
					} else if (json.duty == 2) { //区县管理员
						$('#quanbu').remove();
						$('#sheng').remove();
						$("#cityId").val(json.cityId);
						$("#cityId").attr("disabled", true);
						var data1 = selectCountyId(json.cityId);
						appendCounty(data1);
						$("#countyId").val(json.countyId);
						$("#countyId").attr("disabled", true);
						form.render();
					} else if (json.duty == 3) { //项目管理员
						$('#quanbu').remove();
						$('#sheng').remove();
						$('#dishi').remove();
						var data1 = selectCountyId(json.cityId);
						appendCounty(data1);
						$("#cityId").val(json.cityId);
						$("#cityId").attr("disabled", true);
						$("#countyId").val(json.countyId);
						$("#countyId").attr("disabled", true);
						form.render();
					}

					console.info($("#cityId").val());
					console.info($("#countyId").val());

					selectduty();


				},
				error : function() {
					layer.msg('系统异常，请联系管理员处理!');
				}
			});
		});

		$('#find').on('click', function() {
			table.reload('modelReload', {
				method : 'post',
				page : {
					curr : 1
				},
				where : {
					groupName : $("#groupName").val(),
					cityId : $("#cityId").val(),
					countyId : $("#countyId").val(),
				}
			});
			selectduty();
			console.info("2222");
		});
	});
</script>
<script type="text/html" id="barDemo">
        <a href="#" lay-event="edit">修改</a>&nbsp;&nbsp;&nbsp;
		<a href="#" lay-event="view">详情</a>&nbsp;&nbsp;&nbsp;
        <a href="#" lay-event="del">删除</a>

    </script>
</head>
<body>
	<div class="layui-content-body" style="padding-top:40px">
		<div class="scroll-box">
			<div class="breadcrumb-box">
				<span class="layui-breadcrumb" lay-separator="|"> <a
					href="javascript:returnIndex2()">首页&nbsp;</a> <a href="">
						&nbsp;短信模板管理 </a> <a href=""> &nbsp;分组管理&nbsp; </a>
				</span>
			</div>
			<div class="content-box">
				<div class="form-select-box">
					<div class="layui-form layui-form-pane" action="">
						<div class="layui-form-item">
							<div class="layui-inline">
								<label class="layui-form-label">地市</label>
								<div class="layui-input-block" style="width:150px">
									<select name="cityId" lay-filter="cityId" id="cityId">
										<option value="">全部</option>
										<option value="11">苏州</option>
										<option value="12">淮安</option>
										<option value="13">宿迁</option>
										<option value="14">南京</option>
										<option value="15">连云港</option>
										<option value="16">徐州</option>
										<option value="17">常州</option>
										<option value="18">镇江</option>
										<option value="19">无锡</option>
										<option value="20">南通</option>
										<option value="21">泰州</option>
										<option value="22">盐城</option>
										<option value="23">扬州</option>
									</select>
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">区县</label>
								<div class="layui-input-block" style="width:150px">
									<select name="countyId" lay-filter="countyId" id="countyId">
										<option value="">全部</option>
									</select>
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">分组名称</label>
								<div class="layui-input-block" style="width:150px">
									<input type="text" name="groupName" lay-verify="required"
										placeholder="请输入" autocomplete="off" class="layui-input"
										id="groupName">
								</div>
							</div>
							<div class="layui-inline" pane="">
								<button class="layui-btn layui-btn-normal" id="find">查询</button>
								<button class="layui-btn layui-btn-normal" type="button"
									onclick="addUser()">添加</button>
							</div>
						</div>
					</div>
				</div>
				<div class="table-box">
					<table class="layui-table" id="table" lay-filter="demo">
					</table>
				</div>
			</div>
		</div>
	</div>
</body>
</html>